// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A [author] rhyme_lph
// h [email]  rhymelph@gmail.com
// K [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/income_type_entity.dart';
import 'package:finance_app/src/widgets/button/add_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/select_income_type_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "selectIncomeTypePage")
class SelectIncomeTypePage extends StatefulWidget {
  final String currency;

  const SelectIncomeTypePage({Key key, this.currency}) : super(key: key);

  @override
  _SelectIncomeTypePageState createState() => _SelectIncomeTypePageState();
}

class _SelectIncomeTypePageState extends State<SelectIncomeTypePage>
    with PagePresenter<SelectIncomeTypePage, SelectIncomeTypeProvider> {
  @override
  void initState() {
    provider.currency = widget.currency;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.add_income_type);

  @override
  Widget get buildEmptyWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            NormalFormWrapper(
              leftPadding: getDp(24),
              child: AddButton(
                title: S.current.add_income_type,
                onTap: provider.onNavAddIncomeType,
              ),
            ),
            Expanded(child: super.buildEmptyWidget),
          ],
        ),
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            AppSpaces.h20,
            SearchInputField(
              controller: provider.searchController,
              focusNode: provider.searchFocusNode,
            ),
            NormalFormWrapper(
              leftPadding: getDp(24),
              bottomPadding: getDp(10),
              child: AddButton(
                title: S.current.add_income_type,
                onTap: provider.onNavAddIncomeType,
              ),
            ),
            Expanded(
                child: listBuilder<List<IncomeTypeEntity>>(
              selector: (_, __) => List.from(__.data),
              builder: (BuildContext context, List<IncomeTypeEntity> value,
                      Widget child) =>
                  ListView.builder(
                itemBuilder: _buildItems,
                itemCount: value.length,
              ),
            )),
          ],
        ),
      );

  Widget _buildItems(BuildContext context, int index) {
    return listBuilder<IncomeTypeEntity>(
      selector: (_, __) => __.data[index],
      builder: (BuildContext context, IncomeTypeEntity item, Widget child) =>
          PressedOpacityButton(
        onPressed: () => provider.onSelect(item),
        padding: EdgeInsets.symmetric(
          horizontal: getDp(24),
          vertical: getDp(10),
        ),
        child: listBuilder<bool>(
          selector: (_, __) => __.currentSelect == item,
          builder: (BuildContext context, bool value, Widget child) =>
              NormalRadio(
            isSelect: value,
            title: '${item.name}',
          ),
        ),
      ),
    );
  }
}
